package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.PublicAssetsApplyInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Public assets apply fragment.
 *
 * @author sHadowLess
 */
public class PublicAssetsApplyFragment extends BaseFragment {

    /**
     * The Public assets order id.
     */
    @BindView(R.id.public_assets_orderId)
    TextView publicAssetsOrderId;

    /**
     * The Public assets apply date.
     */
    @BindView(R.id.public_assets_applyDate)
    TextView publicAssetsApplyDate;

    /**
     * The Public assets applier.
     */
    @BindView(R.id.public_assets_applier)
    TextView publicAssetsApplier;

    /**
     * The Public assets apply depart.
     */
    @BindView(R.id.public_assets_applyDepart)
    TextView publicAssetsApplyDepart;

    /**
     * The Public assets position.
     */
    @BindView(R.id.public_assets_position)
    TextView publicAssetsPosition;

    /**
     * The Public assets remark.
     */
    @BindView(R.id.public_assets_remark)
    TextView publicAssetsRemark;

    /**
     * The Public assets child tab.
     */
    @BindView(R.id.public_assets_child_tab)
    LinearLayout publicAssetsChildTab;
    /**
     * The Public assets check process.
     */
    @BindView(R.id.public_assets_check_process)
    LinearLayout publicAssetsCheckProcess;

    private final String TAG = PublicAssetsApplyFragment.class.getSimpleName();

    private String url = "api/PublicResourceRequiredOrder/LoadRequiredHistoryListByOrderId?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_public_assets_apply_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initPublicAssetsApplyInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, publicAssetsCheckProcess);
        }
    }

    @Override
    protected void initView() {

    }

    private void initPublicAssetsApplyInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getPublicAssetsApplyInfo(orderId, userName, new RequestCallBack<PublicAssetsApplyInfo>() {
                    @Override
                    public void success(RequestResult<PublicAssetsApplyInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.getResult().toString());
                                PublicAssetsApplyInfo.DataDTO dataBean = result.getResult().getData();
                                List<PublicAssetsApplyInfo.DataDTO.ItemListDTO> itemList = dataBean.getItemList();
                                String id = dataBean.getId();
                                String createTimeString = dataBean.getCreateTimeString();
                                String applicantName = dataBean.getApplicantName();
                                String departmentName = dataBean.getDepartmentName();
                                String positionName = dataBean.getPositionName();
                                String remark = dataBean.getRemark();
                                publicAssetsOrderId.setText(id);
                                publicAssetsApplyDate.setText(createTimeString);
                                publicAssetsApplier.setText(applicantName);
                                publicAssetsApplyDepart.setText(departmentName);
                                publicAssetsPosition.setText(positionName);
                                publicAssetsRemark.setText(remark);

                                for (PublicAssetsApplyInfo.DataDTO.ItemListDTO itemListDTO : itemList) {
                                    View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_public_assets_apply_tab_item, null, false);
                                    LinearLayout mainTab = view.findViewById(R.id.public_assets_tab);
                                    TextView category = view.findViewById(R.id.public_assets_tab_category);
                                    TextView categoryName = view.findViewById(R.id.public_assets_tab_categoryName);
                                    TextView specifications = view.findViewById(R.id.public_assets_tab_specifications);
                                    TextView tabSize = view.findViewById(R.id.public_assets_tab_size);
                                    TextView tabUnit = view.findViewById(R.id.public_assets_tab_unit);
                                    TextView number = view.findViewById(R.id.public_assets_tab_number);
                                    TextView tabRemark = view.findViewById(R.id.public_assets_tab_remark);

                                    String publicResourceName = itemListDTO.getPublicResourceName();
                                    String publicResourceItemName = itemListDTO.getPublicResourceItemName();
                                    String brand = itemListDTO.getBrand();
                                    String size = itemListDTO.getSize();
                                    String unit = itemListDTO.getUnit();
                                    BigDecimal num = itemListDTO.getNum();
                                    String tRemark = itemListDTO.getRemark();

                                    category.setText(publicResourceName);
                                    categoryName.setText(publicResourceItemName);
                                    specifications.setText(brand);
                                    tabSize.setText(size);
                                    tabUnit.setText(unit);
                                    number.setText(String.valueOf(num.doubleValue()));
                                    tabRemark.setText(tRemark);
                                    publicAssetsChildTab.addView(mainTab);
                                }

                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }
}
